<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
use app\common\model\GzhAccount as  model;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\View;


class GzhAccount extends AdminController {

    public function index(){
        if($this->request->isAjax()){
            $model = new model();
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $res = $model->order('id','desc')
                ->paginate(['list_rows'=>$this->limit,'page'=>$this->page])
                ->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;
        }else{
            return View::fetch();

        }
    }

    public function edit()
    {
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        View::engine()->layout(false);
        $model = new model();
        if($this->request->isPost())
        {
            $data = $this->request->param();
            if($this->request->param('id')){
                unset($data['id']);
                $data['updated_at'] = TIME;
                $model->strict(false)->where('id',$this->request->param('id'))->save($data);
            }else{
                $model->strict(false)->insert($data);
            }

            $this->setActionLog('编辑公众号',$this->admin_info['name'].'：编辑公众号'.$this->request->param('id'));
            $status = [
                'status' => true,
                'msg' => '修改成功',
                'data' => []
            ];
            return  $status;
        }

        if($this->request->param('id'))
        {
            //编辑页面
            $info = $model->where(['id'=>$this->request->param('id')])->find();
            if(!$info)
            {
                return error_code(10000);
            }
            View::assign('data',$info);
        }
        $result['status'] = true;
        $result['msg'] = '修改成功';
        $result['data'] = View::fetch();
        return $result;
    }


    public function add(){

        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        View::engine()->layout(false);
        $model = new model();

        if($this->request->isPost())
        {
            $data = $this->request->param();
            $data['created_at'] = TIME;
            $data['updated_at'] = TIME;
            $id = $model->strict(false)->insertGetId($data);
            $this->setActionLog('添加公众号',$this->admin_info['name'].'：添加公众号'.$id);
            $status = [
                'status' => true,
                'msg' => '添加成功',
                'data' => []
            ];
            return  $status;
        }
        $result['status'] = true;
        $result['msg'] = '成功';
        $result['data'] = View::fetch();
        return $result;
    }


    public function del(){
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        $id = $this->request->param('id');
        $model = new model();
        $res = $model->where('id',$id)->delete();
        if($res) {
            $this->setActionLog('删除公众号',$this->admin_info['name'].'：删除公众号'.$id);
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }else{
            $result['msg'] = '删除失败，请联系管理员';
        }
        return $result;
    }

    public function changeUse(){
        $state  = $this->request->param('state','true');
        $id     = $this->request->param('id',0);
        if ($state == 'true') {
            $state = '2';
        } else {
            $state = '1';
        }
        $model = new model();
        $res = $model->where('id',$id)->save([
            'is_use'=>$state
        ]);
        if($res){
            $this->setActionLog('设置公众号状态',$this->admin_info['name'].'：设置了公众号：'.$id.'的使用状态为：'.$state);
            $result['msg']    = '设置成功';
            $result['status'] = true;
        }else{
            $result['msg']    = '设置失败';
            $result['status'] = false;
        }
        return $result;

    }


    public function import(){

        $file = $this->request->file('importFile');

        if (!$file) {
            return json(['code' => 0, 'msg' => '请上传文件']);
        }

        // 检查文件格式
        $ext = $file->extension();
        if (!in_array($ext, ['xlsx', 'xls'])) {
            return json(['code' => 0, 'msg' => '文件格式不正确']);
        }
        $ext = $file->extension();
        if (!in_array($ext, ['xlsx', 'xls'])) {
            return json(['code' => 0, 'msg' => '文件格式不正确']);
        }

        // 读取 Excel 文件
        $spreadsheet = IOFactory::load($file->getRealPath());
        $sheet = $spreadsheet->getActiveSheet();
        $rows = $sheet->toArray();
        $data = [];
        // 假设第一行是表头，从第二行开始读取数据
        foreach ($rows as $index => $row) {
            if ($index === 0) {
                continue; // 跳过表头
            }

            // 假设 Excel 文件中的列顺序是：姓名, 邮箱, 手机号
            $data[] = [
                'name'  => $row[0],
                'email' => $row[1],
                'psd'   => $row[2],
                'operator'   => $row[3],
                'appid'   => $row[4],
                'secret'   => $row[5],
                'administrator'   => $row[6],
                'created_at'   => TIMESTAMP,
                'upadted_at'   => TIMESTAMP,
            ];

            // 插入数据库
        }
        $model = new model();
        $res = $model->strict(false)->insertAll($data);
        if($res){
            $this->setActionLog('添加公众号',$this->admin_info['name'].'：批量添加了'.sizeof($data).'个公众号');

            $status = [
                'status' => true,
                'msg' => '添加成功',
                'data' => []
            ];
            return  $status;
        }else{
            $status = [
                'status' => false,
                'msg' => '添加失败',
                'data' => []
            ];
            return  $status;
        }
    }

}
